<?php
namespace app\warehouse\admin;
use app\system\admin\Admin;
use app\warehouse\model\Purchase as PurchaseModel;
use app\warehouse\model\Stock as StockModel;
use app\warehouse\model\Direct as DirectModel;
use app\product\model\Product as ProductModel;
use think\Db;
/**
 *  直接出单
 */
class Direct extends Admin
{
    protected $hisiModel = 'Direct';//模型名称[通用添加、修改专用]


    public function add()
    {
        $sn = create_order_sn();
        $this->assign('sn', $sn);
        $admins = Db::name('system_user')
        ->field('id,nick')
        ->select();
        $this->assign('admins', $admins);
        $shops = Db::name('shop')->field('id as value,name as label')->select();
        foreach ($shops as $key => $value) {
            $shops[$key]['children'] = Db::name('warehouse')->field('id as value, name as label')->where('shop_id',$value['value'])->select();
        }
        $this->assign('warehouses', $shops);
        $products = ProductModel::field('*,supplier_id as supplier_text,unit_id as unit_text, id as product_id')->where('status',1)->all();
        $this->assign('products',$products);
        $chukuType = Db::name('directtype')->order('sort asc')->select();
        $this->assign('chukuType', $chukuType);
        return $this->fetch('form');
    }

    public function edit()
    {
        $id = $this->request->param('id','');
        if($id == ''){
            return $this->error('数据不存在');
        }
        $formData = DirectModel::get($id);
        $this->assign('formData',$formData);
        $tableDate = $formData->product_json;
        $this->assign('tableDate',json_decode($tableDate));
        $admins = Db::name('system_user')
        ->field('id,nick')
        ->select();
        $this->assign('admins', $admins);
        $shops = Db::name('shop')->field('id as value,name as label')->select();
        foreach ($shops as $key => $value) {
            $shops[$key]['children'] = Db::name('warehouse')->field('id as value, name as label')->where('shop_id',$value['value'])->select();
        }
        $this->assign('warehouses', $shops);
        $products = ProductModel::field('*,supplier_id as supplier_text,unit_id as unit_text, id as product_id')->where('status',1)->all();
        $this->assign('products',$products);
        $chukuType = Db::name('directtype')->order('sort asc')->select();
        $this->assign('chukuType', $chukuType);
        if(!is_empty($this->request->param('xx')) && $this->request->param('xx') == 2){
            $this->view->engine->layout(false);
            $formData['shop'] = Db::name('shop')->where('id',$formData['shop_id'])->value('name');
            $formData['warehouse'] = Db::name('warehouse')->where('id',$formData['warehouse_id'])->value('name');
            $this->assign('formData',$formData);
            return $this->fetch('dayin');
        }
        return $this->fetch('check');
    }

    public function index()
    {

        if ($this->request->isAjax()) {
            $where      = [];
            $page       = $this->request->param('page/d', 1);
            $limit      = $this->request->param('limit/d', 15);
            $where['shop_id'] = session('shop_id');
            $data['data'] = DirectModel::where($where)
                            ->field('*, admin_id as admin_id_text,shop_id as shop_name,warehouse_id as warehouse_name,out_type as out_type_text')
                            ->page($page)
                            ->limit($limit)
                            ->order('id desc')
                            ->select();
            $data['count'] = DirectModel::where($where)->count('id');
            $data['code'] = 0;
            return json($data);

        }
        return $this->fetch();
    }

    public function addDo()
    {
        $postData = $this->request->post();
        if($postData['admin_id'] == ''){
            return $this->axiosMsg('操作员为必填项',false);
        }
        if($postData['warehouse_id'] == ''){
            return $this->axiosMsg('仓库为必填项',false);
        }
        if(empty($postData['product'])){
            return $this->axiosMsg('没有产品提交',false);
        }
        $sw = $postData['warehouse_id'];
        unset($postData['warehouse_id']);
        $postData['shop_id'] = $sw[0];
        $postData['warehouse_id'] = $sw[1];
        $postData['product_json'] = json_encode($postData['product']);
        if(DirectModel::create($postData) === false){
            return $this->axiosMsg('创建失败',false);
        }
        return $this->axiosMsg('创建成功！',true);
    }
    public function editDo()
    {
        $postData = $this->request->post();
        if($postData['admin_id'] == ''){
            return $this->axiosMsg('采购员为必填项',false);
        }
        if($postData['warehouse_id'] == ''){
            return $this->axiosMsg('仓库为必填项',false);
        }
        if(empty($postData['product'])){
            return $this->axiosMsg('没有产品提交',false);
        }
        $sw = $postData['warehouse_id'];
        unset($postData['warehouse_id']);
        $postData['shop_id'] = $sw[0];
        $postData['warehouse_id'] = $sw[1];

        $products = $postData['product'];
        $postData['product_json'] = json_encode($postData['product']);
        // 启动事务
        Db::startTrans();
        try {
            if($postData['status'] == 2){
                $ins = [];
                $StockModel = new StockModel;
                foreach($products as $key=>$value){
                    $begin_num = $StockModel->getBeginNum($postData['warehouse_id'],$value['product_id']);
                    // $begin_price = $StockModel->getBeginPrice($postData['warehouse_id'],$value['product_id']);
                    // $end_price = $StockModel->getEndPrice($postData['warehouse_id'],$value['product_id'],$value['num'],$value['price1']);

                    $ins[$key]['product_id']    = $value['product_id'];
                    $ins[$key]['product_num']   = $value['num'];
                    $ins[$key]['product_name']  = $value['name'];
                    $ins[$key]['shop_id']       = $postData['shop_id'];
                    $ins[$key]['sn']            = $postData['sn'];
                    $ins[$key]['type']          = '出库';
                    $ins[$key]['detailed_type'] = 7;
                    $ins[$key]['warehouse_id']  = $postData['warehouse_id'];
                    $ins[$key]['createtime']    = time();
                    $ins[$key]['status']        = 1;
                    $ins[$key]['bak']           = isset($value['bak']) ? $value['bak'] : '';
                    $ins[$key]['updatetime']    = time();

                    // $ins[$key]['unit_price']    = $value['price1'];
                    // $ins[$key]['price']         = $value['num'] * $value['price1'];
                    $ins[$key]['begin_num']     = $begin_num;
                    $ins[$key]['end_num']       = $begin_num - $value['num'];
                    // $ins[$key]['begin_price']   = $begin_price;
                    // $ins[$key]['end_price']     = $end_price;
                }
               
                $StockModel->saveAll($ins);
            }
            DirectModel::update($postData);
             // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return $this->axiosMsg($e->getMessage(), false);
        }


        return $this->axiosMsg('保存成功！',true);
    }
    public function check()
    {
        return $this->fetch();
    }

    /**
     * [look 查看]
     * @return [type] [description]
     */
    public function look()
    {
        $type = $this->request->param('type',1);
        $id = $this->request->param('id','');
        if($id == ''){
            $this->error('采购单不存在！');
        }
        $Direct = DirectModel::get($id);
        $products = json_decode($Direct->product_json,true);
        $zong = 0;
        foreach ($products as $key => $value) {
            $products[$key]['jine'] = $value['price1'] * $value['num'];
            $zong += $products[$key]['jine'];
        }
        $this->assign('zong', $zong);
        $this->assign('products', $products);
        $this->assign('id',$id);
        $this->assign('type',$type);
        return $this->fetch();
    }
}


 ?>
